home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / comms / netsoftware / atcp_finger.lha / finger / lprint.c < prev    next >
C/C++ Source or Header  |  1993-08-09  |  8KB  |  288 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)lprint.c    5.13 (Berkeley) 10/31/90";
  39. #endif /* not lint */
  40.  
  41. #include <sys/types.h>
  42. #include <sys/file.h>
  43. #include <sys/stat.h>
  44. #include <time.h>
  45. #include <tzfile.h>
  46. #include <stdio.h>
  47. #include <ctype.h>
  48. #include <paths.h>
  49. #include "finger.h"
  50.  
  51. #define    LINE_LEN    80
  52. #define    TAB_LEN        8        /* 8 spaces between tabs */
  53. #define    _PATH_PLAN    ".plan"
  54. #define    _PATH_PROJECT    ".project"
  55.  
  56. lflag_print()
  57. {
  58.     extern int pplan;
  59.     register PERSON *pn;
  60.  
  61.     for (pn = phead;;) {
  62.         lprint(pn);
  63.         if (!pplan) {
  64.             (void)show_text(pn->dir, _PATH_PROJECT, "Project:");
  65.             if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
  66.                 (void)printf("No Plan.\n");
  67.         }
  68.         if (!(pn = pn->next))
  69.             break;
  70.         putchar('\n');
  71.     }
  72. }
  73.  
  74. lprint(pn)
  75.     register PERSON *pn;
  76. {
  77.     extern time_t now;
  78.     register struct tm *delta;
  79.     register WHERE *w;
  80.     register int cpr, len, maxlen;
  81.     struct tm *tp;
  82.     int oddfield;
  83.     time_t time();
  84.     char *t, *tzn, *prphone();
  85.  
  86.     /*
  87.      * long format --
  88.      *    login name
  89.      *    real name
  90.      *    home directory
  91.      *    shell
  92.      *    office, office phone, home phone if available
  93.      */
  94.     (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
  95.         pn->name, pn->realname, pn->dir);
  96.     (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
  97.  
  98.     /*
  99.      * try and print office, office phone, and home phone on one line;
  100.      * if that fails, do line filling so it looks nice.
  101.      */
  102. #define    OFFICE_TAG        "Office"
  103. #define    OFFICE_PHONE_TAG    "Office Phone"
  104.     oddfield = 0;
  105.     if (pn->office && pn->officephone &&
  106.         strlen(pn->office) + strlen(pn->officephone) +
  107.         sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
  108.         (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
  109.             prphone(pn->officephone));
  110.         oddfield = demi_print(tbuf, oddfield);
  111.     } else {
  112.         if (pn->office) {
  113.             (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
  114.             oddfield = demi_print(tbuf, oddfield);
  115.         }
  116.         if (pn->officephone) {
  117.             (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
  118.                 prphone(pn->officephone));
  119.             oddfield = demi_print(tbuf, oddfield);
  120.         }
  121.     }
  122.     if (pn->homephone) {
  123.         (void)sprintf(tbuf, "%s: %s", "Home Phone",
  124.             prphone(pn->homephone));
  125.         oddfield = demi_print(tbuf, oddfield);
  126.     }
  127.     if (oddfield)
  128.         putchar('\n');
  129.  
  130.     /*
  131.      * long format con't: * if logged in
  132.      *    terminal
  133.      *    idle time
  134.      *    if messages allowed
  135.      *    where logged in from
  136.      * if not logged in
  137.      *    when last logged in
  138.      */
  139.     /* find out longest device name for this user for formatting */
  140.     for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
  141.         if ((len = strlen(w->tty)) > maxlen)
  142.             maxlen = len;
  143.     /* find rest of entries for user */
  144.     for (w = pn->whead; w != NULL; w = w->next) {
  145.         switch (w->info) {
  146.         case LOGGEDIN:
  147.             tp = localtime(&w->loginat);
  148.             t = asctime(tp);
  149. #ifdef linux
  150.             tzset();
  151.             tzn = tzname[1 - daylight];
  152. #else
  153.             tzn = tp->tm_zone;
  154. #endif
  155.             cpr = printf("On since %.16s (%s) on %s",
  156.                 t, tzn, w->tty);
  157.             /*
  158.              * idle time is tough; if have one, print a comma,
  159.              * then spaces to pad out the device name, then the
  160.              * idle time.  Follow with a comma if a remote login.
  161.              */
  162.             delta = gmtime(&w->idletime);
  163.             if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
  164.                 cpr += printf("%-*s idle ",
  165.                     maxlen - strlen(w->tty) + 1, ",");
  166.                 if (delta->tm_yday > 0) {
  167.                     cpr += printf("%d day%s ",
  168.                        delta->tm_yday,
  169.                        delta->tm_yday == 1 ? "" : "s");
  170.                 }
  171.                 cpr += printf("%d:%02d",
  172.                     delta->tm_hour, delta->tm_min);
  173.                 if (*w->host) {
  174.                     putchar(',');
  175.                     ++cpr;
  176.                 }
  177.             }
  178.             if (!w->writable)
  179.                 cpr += printf(" (messages off)");
  180.             break;
  181.         case LASTLOG:
  182.             if (w->loginat == 0) {
  183.                 (void)printf("Never logged in.");
  184.                 break;
  185.             }
  186.             tp = localtime(&w->loginat);
  187.             t = asctime(tp);
  188. #ifdef linux
  189.             tzset();
  190.             tzn = tzname[1 - daylight];
  191. #else
  192.             tzn = tp->tm_zone;
  193. #endif
  194.             if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
  195.                 cpr =
  196.                     printf("Last login %.16s %.4s (%s) on %s",
  197.                     t, t + 20, tzn, w->tty);
  198.             else
  199.                 cpr = printf("Last login %.16s (%s) on %s",
  200.                     t, tzn, w->tty);
  201.             break;
  202.         }
  203.         if (*w->host) {
  204.             if (LINE_LEN < (cpr + 6 + strlen(w->host)))
  205.                 (void)printf("\n   ");
  206.             (void)printf(" from %s", w->host);
  207.         }
  208.         putchar('\n');
  209.     }
  210. }
  211.  
  212. demi_print(str, oddfield)
  213.     char *str;
  214.     int oddfield;
  215. {
  216.     static int lenlast;
  217.     int lenthis, maxlen;
  218.  
  219.     lenthis = strlen(str);
  220.     if (oddfield) {
  221.         /*
  222.          * We left off on an odd number of fields.  If we haven't
  223.          * crossed the midpoint of the screen, and we have room for
  224.          * the next field, print it on the same line; otherwise,
  225.          * print it on a new line.
  226.          *
  227.          * Note: we insist on having the right hand fields start
  228.          * no less than 5 tabs out.
  229.          */
  230.         maxlen = 5 * TAB_LEN;
  231.         if (maxlen < lenlast)
  232.             maxlen = lenlast;
  233.         if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
  234.             lenthis) <= LINE_LEN) {
  235.             while(lenlast < (4 * TAB_LEN)) {
  236.                 putchar('\t');
  237.                 lenlast += TAB_LEN;
  238.             }
  239.             (void)printf("\t%s\n", str);    /* force one tab */
  240.         } else {
  241.             (void)printf("\n%s", str);    /* go to next line */
  242.             oddfield = !oddfield;    /* this'll be undone below */
  243.         }
  244.     } else
  245.         (void)printf("%s", str);
  246.     oddfield = !oddfield;            /* toggle odd/even marker */
  247.     lenlast = lenthis;
  248.     return(oddfield);
  249. }
  250.  
  251. show_text(directory, file_name, header)
  252.     char *directory, *file_name, *header;
  253. {
  254.     register int ch, lastc;
  255.     register FILE *fp;
  256.  
  257.     (void)sprintf(tbuf, "%s/%s", directory, file_name);
  258.     if ((fp = fopen(tbuf, "r")) == NULL)
  259.         return(0);
  260.     (void)printf("%s\n", header);
  261.     while ((ch = getc(fp)) != EOF)
  262.         vputc(lastc = ch);
  263.     if (lastc != '\n')
  264.         (void)putchar('\n');
  265.     (void)fclose(fp);
  266.     return(1);
  267. }
  268.  
  269. vputc(ch)
  270.     register int ch;
  271. {
  272.     int meta;
  273.  
  274.     if (!isascii(ch)) {
  275.         (void)putchar('M');
  276.         (void)putchar('-');
  277.         ch = toascii(ch);
  278.         meta = 1;
  279.     } else
  280.         meta = 0;
  281.     if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
  282.         (void)putchar(ch);
  283.     else {
  284.         (void)putchar('^');
  285.         (void)putchar(ch == '\177' ? '?' : ch | 0100);
  286.     }
  287. }
  288.